package com.zqxq.lottery.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 苦智
 * @date 2018/4/16 18:25
 * @description 七星彩中奖计算
 * @ClassName CalculatorQXC
 */
@Slf4j
@Component
public class CalculatorQXC extends Calculator {

    private static Object[] winningsList = {"A", "B", 1800, 300, 20, 5};

    @Override
    public String winInfo(int[] win, Integer betTimes) {
        return winInfo(win,winningsList,betTimes);
    }

    @Override
    public int[] win(String lotteryNum, String bettingNum, String playType) {
        int[] win = new int[7];
        lotteryNum = lotteryNum.replace(",","");
        //获取所有投注组合
        List allComb = getAllComb(bettingNum);
        for (Object o : allComb) {
            //中奖号码个数
            int hitNum = compare(lotteryNum, o.toString(), 0);
            switch (hitNum){
                case 7:win[0]++;break;  //一等奖
                case 6:win[1]++;break;  //二等奖
                case 5:win[2]++;break;  //三等奖
                case 4:win[3]++;break;  //四等奖
                case 3:win[4]++;break;  //五等奖
                case 2:win[5]++;break;  //六等奖
            }
        }

        return win;
    }


    /**
     * @Author 苦智
     * @Description 获取七星彩所有组合方式
     * @Date 16:21 2018/4/13
     * @Param [bettingNum]
     * @return java.util.Set
     **/
    public List getAllComb(String bettingNum) {
        String[] array = bettingNum.split("\\|");
        List list= new ArrayList();
        for (String s : array[6].split(",")) {
            for (String s1 : array[5].split(",")) {
                for (String s2 : array[4].split(",")) {
                    for (String s3 : array[3].split(",")) {
                        for (String s4 : array[2].split(",")) {
                            for (String s5 : array[1].split(",")) {
                                for (String s6 : array[0].split(",")) {
                                    list.add(s6+s5+s4+s3+s2+s1+s);
                                }
                            }
                        }
                    }
                }
            }
        }
        return list;
    }

    /**
     * @Author 苦智
     * @Description 获取两个字符串中连续相同最多的字符个数
     * @Date 16:21 2018/4/13
     * @Param [str1, str2, start]
     * @return int
     **/
    public int compare(String str1,String str2,int start){
        if(start<0)return 0;
        if(str1==null || str2==null)return 0;
        int len=str1.length()>str2.length()?str2.length():str1.length();
        if(start>=len)return 0;
        char c1,c2;
        int okLen=0;
        List<Integer> l = new ArrayList();
        for(int i=start;i<len;i++){
            c1=str1.charAt(i);
            c2=str2.charAt(i);
            if(c1==c2){
                okLen++;
            }else{
                okLen=0;
            }
            l.add(okLen);
        }
        Integer max = Collections.max(l);
        return max;
    }

    public static void main(String[] args) {
        System.out.println(JSONObject.toJSONString(new CalculatorQXC().win("1,5,2,4,1,6,4","1,5,4,|5,8,6,|2,4,|4,|5,3,|2,4,|4","0")));
    }
}
